home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / 3D_Rendering / Musgrave_Shaders / Source / terranbump.sl < prev    next >
Encoding:
Text File  |  1994-12-08  |  3.4 KB  |  114 lines

  1. /*
  2.  * terranbump.sl - displacement for an Earth-like planet.
  3.  *
  4.  *
  5.  * DESCRIPTION:
  6.  *      When put on a sphere, makes displacements to make Earth-like
  7.  *   mountains and oceans.  The shader works by using a variety of fractal 
  8.  *   turbulence and mottling techniques.
  9.  *      Note that there is a companion surface shader "terran".  You need
  10.  *   to use exactly the same parameters for "terran" and "terranbump" to
  11.  *   get them to work well together.
  12.  *
  13.  *
  14.  * PARAMETERS:
  15.  *    spectral_exp, lacunarity, octaves - control the fractal characteristics
  16.  *                of the bump pattern.
  17.  *    bump_scale - scaling of the mountains
  18.  *    multifractal - zero uses fBm noise, nonzero uses multifractal
  19.  *    dist_scale - scaling for multifractal distortion
  20.  *    offset - elevation offset
  21.  *    sea_level - obvious
  22.  *
  23.  *
  24.  * HINTS:
  25.  *       The default values for the shader assume that the planet is
  26.  *    represented by a unit sphere.  The texture space and/or parameters
  27.  *    to this shader will need to be altered if the size of your planet
  28.  *    is radically different.
  29.  *       For best results, use with the "terran" surface shader, and
  30.  *    add a cloud layer using either "planetclouds" or "venusclouds".
  31.  *
  32.  *
  33.  * AUTHOR: Ken Musgrave.
  34.  *    Conversion to Shading Language and minor modifications by Larry Gritz.
  35.  *
  36.  *
  37.  * REFERENCES:
  38.  *
  39.  *
  40.  * HISTORY:
  41.  *    ???? - original texture developed by F. Ken Musgrave.
  42.  *    Feb 1994 - Conversion to Shading Language by L. Gritz
  43.  *
  44.  * last modified 1 March 1994 by lg
  45.  */
  46.  
  47.  
  48. #ifdef BMRT
  49. #define snoise(x) (2*(noise(x)-0.5))
  50. #else
  51. /* This is because PRMAN's noise has less range than BMRT's */
  52. #define snoise(x) (2.5*(noise(x)-0.5))
  53. #endif
  54.  
  55. #define DNoise(x) ((2*(point noise(x))) - point(1,1,1))
  56. #define VLNoise(Pt,scale) (snoise(DNoise(Pt)+(scale*Pt)))
  57. #define N_OFFSET 0.7
  58. #define VERY_SMALL 0.0001
  59.  
  60.  
  61.  
  62. displacement
  63. terranbump (float spectral_exp = 0.5;
  64.         float lacunarity = 2, octaves = 7;
  65.         float bump_scale = 0.04;
  66.         float multifractal = 0;
  67.         float dist_scale = .2;
  68.         float offset = 0;
  69.         float sea_level = 0;)
  70. {
  71.   float chaos;
  72.   point Ptexture, tp;
  73.   float l, o, a, i, weight;    /* Loop variables for fBm calc */
  74.   float bumpy;
  75.  
  76.   /* Do all shading in shader space */
  77.   Ptexture = transform ("shader", P);
  78.  
  79.   if (multifractal == 0) {    /* use a "standard" fBm bump function */
  80.       o = 1;  l = 1;  bumpy = 0;
  81.       for (i = 0;  i < octaves;  i += 1) {
  82.       bumpy += o * snoise (l * Ptexture);
  83.       l *= lacunarity;
  84.       o *= spectral_exp;
  85.         }
  86.     }
  87.   else {            /* use a "multifractal" fBm bump function */
  88.       /* get "distortion" vector, as used with clouds */
  89.       Ptexture += dist_scale * DNoise (Ptexture);
  90.       /* compute bump vector using MfBm with displaced point */
  91.       o = spectral_exp;  tp = Ptexture;
  92.       weight = abs (VLNoise (tp, 1.5));
  93.       bumpy = weight * snoise (tp);
  94.       for (i = 1;  i < octaves  &&  weight >= VERY_SMALL;  i += 1) {
  95.       tp *= lacunarity;
  96.       /* get subsequent values, weighted by previous value */
  97.       weight *= o * (N_OFFSET + snoise(tp));
  98.       weight = clamp (abs(weight), 0, 1);
  99.       bumpy += snoise(tp) * min (weight, spectral_exp);
  100.       o *= spectral_exp;
  101.     }
  102.     }
  103.  
  104.   /* get the "height" of the bump, displacing by offset */
  105.   chaos = bumpy + offset;
  106.  
  107.   /* set bump for land masses (i.e., areas above "sea level") */
  108.   if (chaos > sea_level)
  109.       P += (bump_scale * bumpy) * normalize(Ng);
  110.  
  111.   /* Recalculate the surface normal (this is where all the real magic is!) */
  112.   N = calculatenormal (P);
  113. }
  114.